package com.yenlien.traceyun.business.trace;


import javax.persistence.Column;
import javax.persistence.Table;
import java.lang.reflect.Field;
import java.util.List;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-07-23
 * @description:
 **/
public class EntityTraceBean implements Comparable<EntityTraceBean> {

    private String appName;

    private int order;

    private String stepName;

    private String className;

    //关联class
    private String relationClass;


    private String mainColumn;

    private String relationColumn;

    private String tableName;

    private String[] parentStepName;

    private String parentStep;

    /**
     * 交叉点
     */
    private String[] corssPront;

    /**
     * 分支
     */
    private String branch;

    /**
     * sql 查询条件
     */
    private String conditions;

    /**
     * sql 语句
     */
    private String sql;

    private String[] sqlParams;

    /**
     * sql 语句
     */
    private String recallsql;

    private String[] recallsqlParams;

    /**
     * 召回占位符
     */
    private boolean recallMessageFormat;


    private List<EntityTraceBean> children;


    public EntityTraceBean(String beanClass) throws Exception {
        Class clazz = Class.forName(beanClass);
        Trace trace = (Trace) clazz.getDeclaredAnnotation(Trace.class);
        Table table = (Table) clazz.getDeclaredAnnotation(Table.class);
        if (clazz.isAnnotationPresent(RelationTrace.class)) {
            RelationTrace relationTrace = (RelationTrace) clazz.getDeclaredAnnotation(RelationTrace.class);
            Class relationClass = relationTrace.relationClass();
            Table relationTable = (Table) relationClass.getDeclaredAnnotation(Table.class);
            this.relationClass = relationTable.name();
            Field mainField = relationClass.getDeclaredField(relationTrace.mainField());
            Field relationField = relationClass.getDeclaredField(relationTrace.relationField());
            if (mainField.isAnnotationPresent(Column.class)) {
                this.mainColumn = mainField.getDeclaredAnnotation(Column.class).name();
            } else {
                this.mainColumn = mainField.getName();
            }
            if (relationField.isAnnotationPresent(Column.class)) {
                this.relationColumn = relationField.getDeclaredAnnotation(Column.class).name();
            } else {
                this.relationColumn = relationField.getName();
            }

        }
        if (clazz.isAnnotationPresent(TraceSql.class)) {
            TraceSql traceSql = (TraceSql) clazz.getDeclaredAnnotation(TraceSql.class);
            sql = traceSql.sql();
            sqlParams = traceSql.sqlParams();
        }
        if (clazz.isAnnotationPresent(RecallSql.class)) {
            RecallSql traceSql = (RecallSql) clazz.getDeclaredAnnotation(RecallSql.class);
            recallsql = traceSql.sql();
            recallsqlParams = traceSql.sqlParams();
            recallMessageFormat = traceSql.messageFormat();
        }

        this.className = beanClass;
        appName = trace.appName();
        order = trace.order();
        stepName = trace.stepName();
        tableName = table.name();
        corssPront = trace.crossPoint();
        branch = trace.branch();
        conditions = trace.conditions();
        parentStepName = trace.parentStepName();
        if (tableName == null || tableName.equals("")) {
            tableName = Class.forName(beanClass).getName();
        }
    }


    public String getBranch() {
        return branch;
    }

    public void setBranch(String branch) {
        this.branch = branch;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public String getStepName() {
        return stepName;
    }

    public void setStepName(String stepName) {
        this.stepName = stepName;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String[] getParentStepName() {
        return parentStepName;
    }

    public void setParentStepName(String[] parentStepName) {
        this.parentStepName = parentStepName;
    }

    public List<EntityTraceBean> getChildren() {
        return children;
    }

    public void setChildren(List<EntityTraceBean> children) {
        this.children = children;
    }

    public String getRelationClass() {
        return relationClass;
    }

    public void setRelationClass(String relationClass) {
        this.relationClass = relationClass;
    }

    public String getMainColumn() {
        return mainColumn;
    }

    public void setMainColumn(String mainColumn) {
        this.mainColumn = mainColumn;
    }

    public String getRelationColumn() {
        return relationColumn;
    }

    public void setRelationColumn(String relationColumn) {
        this.relationColumn = relationColumn;
    }

    public String getParentStep() {
        return parentStep;
    }

    public void setParentStep(String parentStep) {
        this.parentStep = parentStep;
    }

    public String[] getCorssPront() {
        return corssPront;
    }

    public void setCorssPront(String[] corssPront) {
        this.corssPront = corssPront;
    }

    public String getConditions() {
        return conditions;
    }

    public void setConditions(String conditions) {
        this.conditions = conditions;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public String[] getSqlParams() {
        return sqlParams;
    }

    public void setSqlParams(String[] sqlParams) {
        this.sqlParams = sqlParams;
    }

    public String getRecallsql() {
        return recallsql;
    }

    public void setRecallsql(String recallsql) {
        this.recallsql = recallsql;
    }

    public String[] getRecallsqlParams() {
        return recallsqlParams;
    }

    public void setRecallsqlParams(String[] recallsqlParams) {
        this.recallsqlParams = recallsqlParams;
    }

    public boolean isRecallMessageFormat() {
        return recallMessageFormat;
    }

    public void setRecallMessageFormat(boolean recallMessageFormat) {
        this.recallMessageFormat = recallMessageFormat;
    }

    //倒叙排列
    @Override
    public int compareTo(EntityTraceBean o) {
        if (order < o.getOrder()) {
            return 1;
        } else if (order > o.getOrder()) {
            return -1;
        } else {
            return 0;
        }
    }


}
